const xinstalls =  uni.requireNativePlugin('xinstall-plugin')

function xinstall(status, callback){
  try{
    if (status) {
      xinstalls.addInstallEventListener(function(result) {
        result = result.data.uo
        if (result && JSON.stringify(result) !== '{}') {
          let params = JSON.parse(result)
          let url = uni.getStorageSync('url') || ''

          if (plus.runtime.arguments && plus.runtime.arguments != '' && params.url) {
            getApp().globalData.url = decodeURIComponent(params.url)
            callback && callback()
          } else if (!url && params.url ){
            getApp().globalData.url = decodeURIComponent(params.url)
            uni.setStorageSync('url', params.url)
            callback && callback()
          }
        }
      })
    } else {
      xinstalls.addWakeUpEventListener(function(result) {
        result = result.data.uo
        console.log(result)
        if (result && JSON.stringify(result) !== '{}') {
          let params = JSON.parse(result)
          getApp().globalData.url = decodeURIComponent(params.url)
          callback && callback()
        }
      })
    }
  }catch(e){
  }
}

export default xinstall